package com.wqiuqiu.gulimall.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.wqiuqiu.gulimall.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    //在支付宝创建的应用的id
    private String app_id = "9021000134689137";

    // 商户私钥，您的PKCS8格式RSA2私钥
    private String merchant_private_key = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCbet9Xms6E81TPngleNYvBwsJ6TkwGQhN5xlLeatknEaFSdwgYUiAnL6eJoAVod9JNl6c8uLD8RlpGo6+TvZzJ8EEHPFWR2NuPHYji4sCGwrMsgl804PyN1K+1JKUbgO5EX8wLnhMxEa8tr5O7ENbvyBglKlSfvdPY1YtQUJNp0Zj6hxnnlc7mPrr5E2Rd1Ww9PjQVR1fntjCev0ijdwQxbBo8ReZmGBpOQaFB6c+Pz5v/DOgcaaKBsZLcZNsfDMcYbleeqXXNAkIJyxmM3ZjkkdzRzIy+9Y+WmOL+Bu7tuiLjKdX/gju9gGo7FmKZsnT/w//+TMWYh+z+GTHdTjOJAgMBAAECggEAXUhjJ5Rhw7Kad9ZBmJxXtXC8bdT8F+Uc+jrbDUbd8mrJd0ijOrRLRjd5NUEk/YCmVJdgZzYIOdOb5jXe6a0hnKbg3Q596HtLf5ogvr98811tUo8FwBwcapvor63EwbOhQLSZH38/1HjfuXFWGS4JRuOuWN4E17J0n+u/Ifi1+Q4sgscPuGc+VrLGuPUTH8DiBegqDX0jFxQigc5/sL06Y1/XEyRYmVLi9enU3O4MRym+89jVjWt2vMFQRxpWUbXrpVuiKu+/vWduwJHBOyHm+3ud/cqKxBQd/5KE/ezXas+WpPfdZr54jVJlhP1W1KVLeMgCof/OcWH7ooDbyp+sAQKBgQDu/EM0+5uo73KEe8ROwTN3WxPWV6NobbRU4SXQf5i5coSp4dgkdg6ieqJwYrlYkaxZrc7O7hhFZmmleP1YJ9dctqEJCS5lGIk82t9Ti9gl7uEuDBUr+5kurROgPI2P346FHjNr9BT2WkRLUYfQKtUvxWIbCSKgkD5FWAhOZAsjWQKBgQCmjKTBxIDMIUrMG0+8JlILwcEMtgqKP0KJxYKKvVUGxTEftJ5UNXG3GvKdHQxB75ERRL5S2dQfikZU+8oS9KD9Pd4iN2bkCMCcVdNVal6oXUVt3VwRjNWsDCds1oEzkPaiysD1id13usVzm3ZT0Dy+BtjjLHanwlWMvk78S7d7sQKBgQDt/WkwKiApa8qvBFHJ27xU7+4hVcT1Ccq7fDscFvUQOGrEgpOsO6T3SQjawnWxLeBkriF1EMY61nH+xy4DzCk7uuNDDFGXe4evgodDu8/Udr7oDEBJPJSfVtxkheizjf1vs1Oh/b+iVOIN33NQaldC6113bvF81CgvBiu/ZQFygQKBgE50gkgk4OdH54wucWkYRB31r5LlTiHBmv57Y/snJR6DVZix5MOWSXMP5qnMPTHyQEv6Lg2pPxt8b5Uo5w/23L9unW1Q288fsFv0shDSgrSc+0+9rtk1lpHtR8UlZsd7yjL1f+AqiYEVIl5LJXpl5bc+UnzxPRPZn6MlIuAGavCBAoGAaCH1UYeVXgRl223fBOQL680/kAYRH+U1C023zXlgVAZYJe8am11UqtRxgSYN6LcMZzZSPPtcPcvqh/g6V5aO14QBeL+rK6cIvFInaVHYVEQ0ricOx43XrEE77+UT3DSr50jo6raLyuMfTUUkCYfJ8hiFeF/jUsg9rNcRo1gwuck=";
    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    private String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAnXvQqF+fmuygZJ3tvu9IeGce8kRnkt34LTIr4vpfg9aXbVEcHAlYlpnFzU6rWqvmAnwkSZHCmQBhq/jQR1BOrFuL6xfIJ0GB7rfr8v5oF3wmSOi/ARGv3UoHL5nGTQR1onwq+trV+gUpNi6FA5Y3c1QyLG3LZZZozAhcTT26MOUS7o8fa+HeymDVggtISFTjU/i2tRa5C1kAiNkvyzgYpR3EmULEBjqm1PdXmY7VuwSabdkzyU4k9Ou05ePZIhwgnUokl8B5/z/DM3ro7B4XK5ALtVaSjMoNGIwFjhr5CeFlBteJZP1KKks4851F2PFf3UCcFe6Pvx5ZVUicD1tQNQIDAQAB";
    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    private String notify_url = "http://58a95c94.r6.cpolar.top/aliPayed/message";

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页  跳转到用户的订单页
    private String return_url = "http://order.gulimall.com/memberOrder.html";

    // 签名方式
    private String sign_type = "RSA2";

    // 字符编码格式
    private String charset = "utf-8";

    private String timeout = "1m";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private String gatewayUrl = "https://openapi-sandbox.dl.alipaydev.com/gateway.do";

    public String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\"" + out_trade_no + "\","
                + "\"total_amount\":\"" + total_amount + "\","
                + "\"subject\":\"" + subject + "\","
                + "\"body\":\"" + body + "\","
                + "\"timeout_express\":\"" + timeout + "\"," //订单支付超时时间
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应：" + result);

        return result;

    }
}
